package com.kanshu.ksgb.fastread.doudou.common.business.errorlog.utils;

import com.kanshu.ksgb.fastread.doudou.app.constants.UrlConfig;
import com.kanshu.ksgb.fastread.doudou.common.business.manager.MMKVDefaultManager;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.ErrorLogRequest;
import com.kanshu.ksgb.fastread.doudou.common.net.retrofit.RetrofitHelper;
import com.kanshu.ksgb.fastread.doudou.common.util.JsonUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;

import org.json.JSONObject;

import java.io.IOException;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * Title: 错误日志工具类
 * Description: ErrorLogUtil.java
 * Copyright: Copyright(c)2018
 * Company: 成都古羌科技有限公司
 * CreateTime: 2018/3/14 15:33
 *
 * @author EvanHe
 * @version 1.3
 */

public class ErrorLogUtil {

    static OkHttpClient sOkHttpClient;

    public static void httpLogReportByOkHttp(ErrorLogRequest bean) {
        if (!MMKVDefaultManager.getInstance().isHttpLogSwitch()) {
            return;
        }
        Map<String, String> map = JsonUtils.jsonStrToMap(bean.toJsonStr());
        if (sOkHttpClient == null) {
            sOkHttpClient = new OkHttpClient();
        }
        String url = Utils.linkUrl(UrlConfig.APP_ERROR_LOG_HOST + "yd/exceptionlog/report", map);
        Request request = new Request.Builder().url(url).build();
        Call call = sOkHttpClient.newCall(request);
        try {
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    if (e == null) {
                        return;
                    }
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                }
            });
        } catch (OutOfMemoryError error) {

        }


    }

    public static void httpLogSwitch() {
        RetrofitHelper helper = RetrofitHelper.getInstance();
        //helper.setRetrofit(new ErrorLogRetrofit());
        helper.createService(ErrorLogRetrofit.class, ErrorLogService.class)
                .errorLogSwitch()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io()).subscribe(new Observer<ResponseBody>() {
            @Override
            public void onSubscribe(Disposable disposable) {

            }

            @Override
            public void onNext(ResponseBody responseBody) {
                String s = null;
                try {
                    s = responseBody.string();
                    JSONObject object = new JSONObject(s);
                    JSONObject result = object.getJSONObject("result");
                    JSONObject status = result.getJSONObject("status");
                    int code = status.getInt("code");
                    String str = status.getString("msg");
                    JSONObject data = result.getJSONObject("data");
                    int aSwitch = data.optInt("switch");
                    int timeOut = data.getInt("timeout");
                    MMKVDefaultManager.getInstance().setHttpLogSwitch(aSwitch == 1);
                    MMKVDefaultManager.getInstance().setHttpTimeoutUpload(timeOut);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onError(Throwable throwable) {
            }
            @Override
            public void onComplete() {

            }
        });
    }
}
